﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xeml.Document.Contracts;
using Xeml.Document;
using Xeml.Sampling.Contracts;

namespace XemlInteractiveDesigner
{
    public partial class DataProviderSelectorForm : Form
    {

        public DataProviderResource SelectedDataProvider { get; private set; }

        public DataProviderSelectorForm(IDocument xemlDoc)
        {
            InitializeComponent();
            cbDataProvider.DataSource = new BindingSource(xemlDoc.DocumentResources.DataProvider.ToDictionary(x => x.Provider.ComponentName, x => x), null);
            cbDataProvider.DisplayMember = "Key";
            cbDataProvider.ValueMember = "Value";
        }

        private void cbDataProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            //SelectedDataProvider = ((KeyValuePair<string, DataProviderResource>)cbDataProvider.SelectedValue).Value;
            // for some reason this returns different values on startup than on selection?
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SelectedDataProvider = (DataProviderResource)cbDataProvider.SelectedValue;
        }

        public static DataProviderResource GetADataProvider(IDocument doc)
        {
            if (doc.DocumentResources.DataProvider.Count > 1)
            {
                DataProviderSelectorForm dataProviderSelectorForm = new DataProviderSelectorForm(doc);
                if (dataProviderSelectorForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    return dataProviderSelectorForm.SelectedDataProvider;
                }
                else
                {
                    throw new OperationCanceledException();
                }
            }

            if (doc.DocumentResources.DataProvider.Count == 0)
            {
                if (MessageBox.Show("Please add a data provider first!", "Data provider missing", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    using (Xid.Controls.DataConnectionWizard dcwiz = new Xid.Controls.DataConnectionWizard())
                    {
                        dcwiz.ShowDialog();
                        if (dcwiz.ProviderResource != null)
                        {
                            doc.DocumentResources.DataProvider.Add(dcwiz.ProviderResource);
                        }
                        else
                        {
                            throw new OperationCanceledException();
                        }
                    }
                }
                else
                {
                    throw new OperationCanceledException();
                }
            }

            return doc.DocumentResources.DataProvider.FirstOrDefault();
        }
    }
}
